/*-------------------------------------------------------------------------
   Copyright (C) 2010 One Laptop per Child
 ------------------------------------------------------------------------- */
#ifndef AC_H
#define AC_H

#include "adc.h"

void ac_init(void);
void ac_process_adapter_status(void);
bool is_ac_adapter_in(void);
void ac_monitor_enable(bool);
void ac_config_unplugged(void);
void ac_cmd_read_Vin(void);
void ac_cmd_read_Vin_scaled(void);
void ac_adjust_input_power(void);

// Vin is sampled every main loop iterating so the avg depth can be quite large to get
// a reading thats fairly stable.
#define VIN_AVG_DEPTH        512
#define VIN_AVG_SCALE        10
#define VIN_AVG_INIT		( Vin_avg = ((unsigned long)read_ad_ch(ADC_CHRG_AC_IN)*VIN_AVG_SCALE) \
					* VIN_AVG_DEPTH )
#define DO_VIN_AVG		( Vin_avg = Vin_avg - (Vin_avg / VIN_AVG_DEPTH) \
					+ ((unsigned long)read_ad_ch(ADC_CHRG_AC_IN)*VIN_AVG_SCALE) )

#define AVG_VIN                    ((int)(Vin_avg / VIN_AVG_DEPTH))

// Scaling constants derived from lab measurements

// Order matters to keep the numbers inside the range for a long
#define VIN_IN_DECI_V		((int)(( ((Vin_avg/VIN_AVG_DEPTH) * 6305) + 5716455L)/1000000L))

extern unsigned int 	xdata Vin;
extern long 			data Vin_avg;

#endif
